home *** CD-ROM | disk | FTP | other *** search
- ///--------------------------------------------------------------------------------------
- // Application.c
- //
- // Created: Sunday, April 11, 1993
- // By: Tony Myles
- //
- // Copyright: © 1993 Tony Myles, All rights reserved worldwide.
- ///--------------------------------------------------------------------------------------
-
-
- #if THINK_C
- #ifndef __BDC__
- #include <BDC.h>
- #endif
- #else
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
- #endif
-
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
-
- #ifndef __DESK__
- #include <Desk.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __DISKINIT__
- #include <DiskInit.h>
- #endif
-
- #ifndef __EPPC__
- #include <EPPC.h>
- #endif
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- #ifndef __GESTALTEQU__
- #include <GestaltEqu.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __OSEVENTS__
- #include <OSEvents.h>
- #endif
-
- #ifndef __TEXTEDIT__
- #include <TextEdit.h>
- #endif
-
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
-
- #ifndef __SPRITEWORLD__
- #include <SpriteWorld.h>
- #endif
-
- #ifndef __APPLICATION__
- #include "Application.h"
- #endif
-
- #ifndef __SPACEGAME__
- #include "SpaceGame.h"
- #endif
-
- #ifndef __DEBUGUTILS__
- #include "DebugUtils.h"
- #endif
-
-
- Boolean gIsRunning = true;
- Boolean gInBackGround = false;
- Boolean gHasWaitNextEvent = false;
- WindowPtr gWindowP = NULL;
- SpaceGamePtr gSpaceGameP = NULL;
-
-
- void main(void)
- {
- OSErr err = noErr;
-
- Initialize(kNumberOfMoreMastersCalls);
-
- if (CheckSystem())
- {
- if (HasAppleEvents())
- {
- err = InstallAppleEventHandlers();
-
- if (err != noErr)
- {
- ErrorAlert(err, kUnknownErrorStringIndex);
- }
- }
-
- CreateMenuBar();
-
- if (EnterApplication())
- {
- ServiceEvents();
- }
-
- ExitApplication();
- }
- else
- {
- CantRunOnThisMachine();
- }
-
- ExitToShell();
- }
-
-
- void Initialize(
- short numberOfMasters)
- {
- EventRecord tempEvent;
-
- MaxApplZone();
-
- while (numberOfMasters--)
- {
- MoreMasters();
- }
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- FlushEvents(everyEvent, 0);
-
- (void)EventAvail(everyEvent, &tempEvent);
- (void)EventAvail(everyEvent, &tempEvent);
- (void)EventAvail(everyEvent, &tempEvent);
-
- gHasWaitNextEvent = HasWaitNextEvent();
- }
-
-
- Boolean CheckSystem(void)
- {
- OSErr err;
- Boolean isSystemGood = true;
- long gestaltResult;
-
- err = Gestalt(gestaltTimeMgrVersion, &gestaltResult);
-
- isSystemGood = (err == noErr) && (gestaltResult >= gestaltStandardTimeMgr);
-
- if (!isSystemGood)
- {
- CantRunOnThisMachine();
- }
-
- return isSystemGood;
- }
-
-
- Boolean HasAppleEvents(void)
- {
- Boolean hasAppleEvents;
- OSErr err;
- long gestaltResult;
-
- err = Gestalt(gestaltAppleEventsAttr, &gestaltResult);
-
- if (err == noErr)
- {
- hasAppleEvents = (gestaltResult & (1 << gestaltAppleEventsPresent)) != 0;
- }
- else
- {
- hasAppleEvents = false;
- }
-
- return hasAppleEvents;
- }
-
-
- OSErr InstallAppleEventHandlers(void)
- {
- OSErr err = noErr;
-
- err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, HandleOpenApp, 0, false);
-
- if (err == noErr)
- {
- err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, HandleOpenDoc, 0, false);
- }
-
- if (err == noErr)
- {
- err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, HandlePrintDoc, 0, false);
- }
-
- if (err == noErr)
- {
- err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, HandleQuit, 0, false);
- }
-
- return err;
- }
-
-
- void CreateMenuBar(void)
- {
- Handle menuBarH;
-
- menuBarH = GetNewMBar(kMenuBarResID);
-
- if (menuBarH != NULL)
- {
- SetMenuBar(menuBarH);
- AddResMenu(GetMHandle(kAppleMenuID), 'DRVR');
- DrawMenuBar();
- }
- else
- {
- CantFindResource();
- }
- }
-
-
- void CreateWindow(void)
- {
- gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
-
- if (gWindowP != NULL)
- {
- SizeWindow(gWindowP, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom, false);
- MoveWindow(gWindowP, 0, 0, false);
- }
- else
- {
- CantFindResource();
- }
- }
-
-
- Boolean EnterApplication(void)
- {
- OSErr err;
- short oldResRefNum, newResRefNum;
- Str255 fileName;
-
- SetCursor(*GetCursor(watchCursor));
-
- GetIndString(fileName, kGameStringsResID, kGraphicsFileNameStringIndex);
-
- if (fileName[0] == 0)
- {
- CantFindResource();
- }
-
- oldResRefNum = CurResFile();
- newResRefNum = OpenResFile(fileName);
- err = ResError();
-
- if (err == noErr)
- {
- UseResFile(newResRefNum);
- }
-
- if (err == noErr)
- {
- err = EnterSpaceGame();
- }
-
- if (err == noErr)
- {
- err = CreateSpaceGame(&gSpaceGameP);
- }
-
- if (newResRefNum != -1)
- {
- UseResFile(oldResRefNum);
- CloseResFile(newResRefNum);
- }
-
- SetCursor(&qd.arrow);
-
- if (err != noErr)
- {
- ErrorAlert(err, kUnknownErrorStringIndex);
- }
-
- return err == noErr;
- }
-
-
- void ExitApplication(void)
- {
- if (gSpaceGameP != NULL)
- {
- DisposeSpaceGame(gSpaceGameP);
- gSpaceGameP = NULL;
- }
-
- ExitSpaceGame();
-
- if (gWindowP != NULL)
- {
- DisposeWindow(gWindowP);
- gWindowP = NULL;
- }
- }
-
-
- void ServiceEvents(void)
- {
- Boolean haveEvent;
- EventRecord event;
- long sleepTime;
- RgnHandle mouseRgn = gHasWaitNextEvent ? NewRgn() : NULL;
-
- while (gIsRunning)
- {
- if (gHasWaitNextEvent)
- {
- sleepTime = gInBackGround ? kBackGroundSleepTime : kForeGroundSleepTime;
-
- haveEvent = WaitNextEvent(everyEvent, &event, sleepTime, mouseRgn);
- }
- else
- {
- SystemTask();
- haveEvent = GetNextEvent(everyEvent, &event);
- }
-
- if (haveEvent)
- {
- DispatchEvent(&event);
- }
- else
- {
- HandleNullEvent();
- }
- }
-
- if (mouseRgn != NULL)
- {
- DisposeRgn(mouseRgn);
- }
- }
-
-
- void DispatchEvent(
- EventRecord* event)
- {
- switch(event->what)
- {
- case mouseDown: HandleMouseEvent(event);
- break;
- case mouseUp:
- break;
- case keyUp:
- break;
- case keyDown:
- case autoKey: HandleKeyEvent((char)(event->message & charCodeMask), event->modifiers);
- break;
- case updateEvt: HandleUpdateEvent((WindowPtr)event->message);
- break;
- case diskEvt: HandleDiskEvent(event->message);
- break;
- case activateEvt: HandleActivateEvent((WindowPtr)event->message);
- break;
- case networkEvt:
- break;
- case driverEvt:
- break;
- case app1Evt:
- break;
- case app2Evt:
- break;
- case app3Evt:
- break;
- case osEvt: HandleOSEvent(event->message);
- break;
- case kHighLevelEvent: AEProcessAppleEvent(event);
- default:
- break;
- }
- }
-
-
- void HandleMouseEvent(
- EventRecord* event)
- {
- WindowPtr whichWindow;
- short partCode;
-
- partCode = FindWindow(event->where, &whichWindow);
-
- switch (partCode)
- {
- case inDesk:
- break;
- case inMenuBar: HandleMenuCommand(MenuSelect(event->where));
- break;
- case inSysWindow: SystemClick(event, whichWindow);
- break;
- case inContent:
- break;
- case inDrag:
- break;
- case inGrow:
- break;
- case inGoAway:
- break;
- case inZoomIn:
- case inZoomOut:
- break;
- default:
- break;
- }
- }
-
-
- void HandleKeyEvent(
- char key,
- short modifiers)
- {
- if ((modifiers & cmdKey) != 0)
- {
- HandleMenuCommand(MenuKey(key));
- }
- else
- {
- // do whatever
- }
- }
-
-
- void HandleUpdateEvent(
- WindowPtr updateWindowP)
- {
- if (updateWindowP == gWindowP)
- {
- SetPort(updateWindowP);
- BeginUpdate(updateWindowP);
-
- if (gSpaceGameP != NULL)
- {
- SWLockSpriteWorld(gSpaceGameP->gameSpriteWorldP);
- SWUpdateSpriteWorld(gSpaceGameP->gameSpriteWorldP);
- SWUnlockSpriteWorld(gSpaceGameP->gameSpriteWorldP);
- }
-
- EndUpdate(updateWindowP);
- }
- }
-
-
- void HandleActivateEvent(
- WindowPtr updateWindowP)
- {
- }
-
-
- void HandleOSEvent(
- long message)
- {
- if ((message >> 24) == suspendResumeMessage)
- {
- if ((message & resumeFlag) != 0)
- {
- gInBackGround = false;
- }
- else
- {
- gInBackGround = true;
- }
- }
- }
-
-
- void HandleDiskEvent(
- long message)
- {
- OSErr err;
- Point dialogLocation = {100, 100};
-
- if ((message & 0xFFFF0000) != noErr)
- {
- err = DIBadMount(dialogLocation, message);
-
- if (err != noErr)
- {
- ErrorAlert(err, kUnknownErrorStringIndex);
- }
- }
- }
-
-
- void HandleNullEvent(void)
- {
- if (gSpaceGameP != NULL)
- IdleSpaceGame(gSpaceGameP);
- }
-
-
- void HandleMenuCommand(
- long menuItemIdentifier)
- {
- short menuIdent = HiWord(menuItemIdentifier);
- short menuItem = LoWord(menuItemIdentifier);
-
- switch (menuIdent)
- {
- case kAppleMenuID:
- {
- HandleAppleMenuCommand(menuItem);
- break;
- }
-
- case kFileMenuID:
- {
- HandleFileMenuCommand(menuItem);
- break;
- }
-
- case kEditMenuID:
- {
- HandleEditMenuCommand(menuItem);
- break;
- }
- }
-
- HiliteMenu(0);
- }
-
-
- void HandleAppleMenuCommand(
- short menuItem)
- {
- Str255 deskAccName;
-
- switch (menuItem)
- {
- case kAboutItem:
- {
- SysBeep(1);
- break;
- }
-
- default:
- {
- GetItem(GetMHandle(kAppleMenuID), menuItem, deskAccName);
- OpenDeskAcc(deskAccName);
- break;
- }
- }
- }
-
-
- void HandleFileMenuCommand(
- short menuItem)
- {
- switch (menuItem)
- {
- case kNewItem:
- {
- if (gSpaceGameP != NULL)
- StartSpaceGame(gSpaceGameP);
- break;
- }
-
- case kQuitItem:
- {
- gIsRunning = false;
- break;
- }
- }
- }
-
-
- void HandleEditMenuCommand(
- short menuItem)
- {
- (void)SystemEdit(menuItem);
- }
-
-
- pascal OSErr HandleOpenApp(
- AppleEvent srcAppleEvent,
- AppleEvent replyAppleEvent,
- long refCon)
- {
- return noErr;
- }
-
-
- pascal OSErr HandleOpenDoc(
- AppleEvent srcAppleEvent,
- AppleEvent replyAppleEvent,
- long refCon)
- {
- return errAEEventNotHandled;
- }
-
-
- pascal OSErr HandlePrintDoc(
- AppleEvent srcAppleEvent,
- AppleEvent replyAppleEvent,
- long refCon)
- {
- return errAEEventNotHandled;
- }
-
-
- pascal OSErr HandleQuit(
- AppleEvent srcAppleEvent,
- AppleEvent replyAppleEvent,
- long refCon)
- {
- gIsRunning = false;
-
- return noErr;
- }
-
-
- void ErrorAlert(
- OSErr err,
- short errorStringIndex)
- {
- Str255 messageString, errorString;
-
- GetIndString(messageString, kErrorStringListResID, errorStringIndex);
-
- if (messageString[0] == 0)
- {
- BlockMove(kSeriousDamageString, messageString, sizeof(kSeriousDamageString));
- }
-
- NumToString(err, errorString);
-
- ParamText(messageString, errorString, "\p", "\p");
-
- (void)StopAlert(kErrorAlertResID, NULL);
- }
-
-
- void CantFindResource(void)
- {
- OSErr err;
-
- err = ResError();
-
- if (err == noErr)
- {
- err = resNotFound;
- }
-
- ErrorAlert(err, kCantFindResourceStringIndex);
-
- ExitToShell();
- }
-
-
- void CantRunOnThisMachine(void)
- {
- (void)StopAlert(kCantRunAlertResID, NULL);
- }
-
-
- short NumToolboxTraps(void)
- {
- return (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
- ? 0x0200 : 0x0400;
- }
-
-
- TrapType GetTrapType(
- short trap)
- {
- #define TrapMask 0x0800
-
- return ((trap & TrapMask) != 0) ? ToolTrap : OSTrap;
- }
-
-
- Boolean TrapAvail(
- short trap)
- {
- TrapType tType;
-
- tType = GetTrapType(trap);
- if (tType == ToolTrap)
- {
- trap = trap & 0x07FF;
- }
-
- if (trap >= NumToolboxTraps())
- {
- trap = _Unimplemented;
- }
-
- return NGetTrapAddress(trap, tType) !=
- NGetTrapAddress(_Unimplemented, ToolTrap);
- }
-
-
- Boolean HasWaitNextEvent(void)
- {
- return TrapAvail(_WaitNextEvent);
- }
-
-
-